package gw.com.android.app.dispatch;

import android.app.Activity;
import android.content.Intent;

import com.gwtsz.gts2.hx.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppContances;
import gw.com.android.app.AppMain;
import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigSettingDeal;
import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import gw.com.android.presenter.PhoneLoginPresenter;
import gw.com.android.terminal.GTSDataListener;
import gw.com.android.ui.MainActivity;
import gw.com.android.ui.me.FeedbackActivity;
import gw.com.android.ui.me.PhoneNumber;
import www.com.library.app.AppActivities;
import www.com.library.app.Logger;
import www.com.library.app.ObjectSessionStore;
import www.com.library.model.DataItemDetail;
import www.com.library.util.AESCrypto2;

/**
 * 分发“我的”模块的业务处理类
 * hxgts2://mine/
 *
 * @author jett
 * @since 2018-05-17.
 */
public class MineDispatch implements Dispatch {

    @Override
    public boolean handle(Activity activity, String path, Map<String, String> params) {
        if ("supply_info".equals(path)) {
            supplyInfo();
            return true;
        } else if ("deposit".equals(path)) {
            deposit(activity);
            return true;
        } else if ("withdraw".equals(path)) {
            withdraw(activity);
            return true;
        } else if ("tab".equals(path)) {
            toMineTab();
            return true;
        } else if ("feedback".equals(path)) {
            feedback(activity);
            return true;
        }
        return false;
    }

    /**
     * hxgts2://mine/tab/
     */
    private void toMineTab() {
        AppActivities.getSingleton().popAllActivityExceptOne(MainActivity.class.getSimpleName());
        MainActivity mainActivity = (MainActivity) ObjectSessionStore.getObject(MainActivity.class.getSimpleName() + GTSDataListener.mainNum);
        if (mainActivity != null) {
            mainActivity.setTabFragment(ConfigType.TAB_MYSELF_TAG, "");
        }
    }

    /**
     * hxgts2://mine/deposit/
     */
    private void deposit(Activity context) {
        if (GTConfig.instance().getAccountType() == GTConfig.ACC_TYPE_GUEST || GTConfig.instance().getAccountType() == GTConfig.ACC_TYPE_DEMO) {
            //LoginDialog.showLoginDialog(context, ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1014));
            return;
        }
        ConfigSettingDeal mSettingDeal = new ConfigSettingDeal();
        ActivityManager.showWebPageActivity(context, mSettingDeal.getItemValue(ConfigType.DEPOSIT_TAG), AppMain.getAppString(R.string.btn_back));
    }

    /**
     * hxgts2://mine/withdraw/
     */
    private void withdraw(Activity context) {
        if (GTConfig.instance().getAccountType() == GTConfig.ACC_TYPE_GUEST || GTConfig.instance().getAccountType() == GTConfig.ACC_TYPE_DEMO) {
            //LoginDialog.showLoginDialog(context, ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1014));
            return;
        }
        ConfigSettingDeal mSettingDeal = new ConfigSettingDeal();
        ActivityManager.showWebPageActivity(context, mSettingDeal.getItemValue(ConfigType.DRAW_TAG), AppMain.getAppString(R.string.btn_back));
    }

    /**
     * hxgts2://mine/feedback/
     */
    private void feedback(Activity content) {
        content.startActivity(new Intent(content, FeedbackActivity.class));
    }

    /**
     * hxgts2://mine/supply_info/
     * 打开补充资料
     */
    private void supplyInfo() {
        // 先拿手机号
        PhoneNumber.getInstance().acquireAsync(new PhoneNumber.OnPhoneListener() {
            @Override
            public void onSuccess(String num) {
                int mSeq = GTSDataListener.curSeq++;
                String mPassword = GTConfig.instance().mUserPwd;
                // 再拿 UUID
                PhoneLoginPresenter.instance().PhoneLoginFun(AppContances.NORMALREQ_TYPE_GET_CUSTOMERBYMOBILE, mSeq, num, mPassword,
                        new PhoneLoginPresenter.OnMobileReqResultListener() {
                            @Override
                            public void reqResultSuccess(Object mObject) {
                                // 最后才是打开补充资料，很烦。。。
                                openSupplyInfo((JSONObject) mObject);
                            }

                            @Override
                            public void reqResultFail(int failCode) {
                                Logger.e("open supply info , an error occured when get uuid");
                            }
                        });
            }
        });
    }

    private void openSupplyInfo(JSONObject jsonResult) {
        try {
            JSONObject resultObj = jsonResult.getJSONObject("result");
            String uuid = resultObj.getString("uuid");
            String mobilePhone = resultObj.getString("mobilePhone");

            DataItemDetail item = new DataItemDetail();
            String url = ConfigUtil.instance().getUrlPath(ConfigType.RELATED_REAL_ACCOUNT);
            try {
                url = url.replace(ConfigType.REPLACE_LOGIN_TAG, AESCrypto2.Encrypt(GTConfig.instance().mCurName));
                url = url.replace(ConfigType.REPLACE_MOBILE_PHONE_TAG, AESCrypto2.Encrypt(mobilePhone));
                url = url.replace(ConfigType.REPLACE_UUID_TAG, AESCrypto2.Encrypt(uuid));
                url = url + "&utm_terminal=android";
            } catch (Exception ignore) {
            }
            item.setStringValue(ConfigType.CONFIG_TYPE_TYPE_URL_TAG, url);
            item.setStringValue(ConfigType.CONFIG_TYPE_TYPE_TAG, ConfigType.CONFIG_TYPE_TYPE_URL_TAG);
            item.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, AppMain.getAppString(R.string.btn_add_info));
            Activity activity = AppActivities.getSingleton().currentActivity();
            if (activity != null) {
                ActivityManager.showWebPageActivity(activity, item, AppMain.getAppString(R.string.btn_back));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
